/*
 *  $Id: HowlingSpirit.cs 1133 2010-09-29 15:42:17Z thenn.erannor $
 *  This file is a part of Ragima CCG.
 *  (C) 2008 Ragima development group.
 *
 *  Licensed under the terms of the GNU GPL License version 2.
 *
 */

namespace Ragima.Cards001.Air.Spells {
    public class HowlingSpirit : SelfEnchantmentCard {
        public HowlingSpirit() : base("001_a_s_howling_spirit") {
            Version = 1;
            Level = CompleteLevel.ReadyForTest;
            Element = ElementType.Air;
            Value = 100;
            AddManaCost(ElementType.Air, 2);
        }

        protected override EnchantmentAffect CreateEnchantment() {
            return new HowlingSpiritAffect();
        }

        private const int HowlingSpiritCost = 1;

        private class HowlingSpiritAffect : EnchantmentAffect<HowlingSpirit> {
            private static readonly Languages.CardPhrase<HowlingSpirit> Pay = new Languages.CardPhrase<HowlingSpirit>("pay");
            private static readonly Languages.CardPhrase<HowlingSpirit> NotPay = new Languages.CardPhrase<HowlingSpirit>("not_pay");

            public override void SetAffect() {
                base.SetAffect();
                GlobalSummary.OnAttackTry += DoAttack;
                EnemySummary.OnAttackTry += DoAttack;
            }
            public override void ResetAffect() {
                base.ResetAffect();
                GlobalSummary.OnAttackTry -= DoAttack;
                EnemySummary.OnAttackTry -= DoAttack;
            }

            private void DoAttack(Creature attacker, ref bool canAttack) {
                bool mana = attacker.Owner.GetMana(attacker.Card.Element) >= HowlingSpiritCost;
                Region region = attacker.FindRegion();
                PlayerCreatureRegion enemy = (PlayerCreatureRegion) region;
                if (mana) {
                    UI.ParticleInfo info = new UI.ParticleInfo(Data.Particles.Shine01);
                    info.AddPhase(0, 5, attacker.Card.Element.ElementColor, 0.2, 0.1, 0, UI.ParticleTrajectory.Line(enemy.IsEnemy ? 90 : -90, 1.5));
                    info.AddPhase(18, 40, attacker.Card.Element.ElementColor, 0.7, 0.4);
                    info.AddPhase(12, 0, attacker.Card.Element.ElementColor, 0.1, 0.2);
                    UI.ParticleEmitterInfo emitter = new UI.ParticleEmitterInfo();
                    emitter.Duration = 70;
                    emitter.Rate = UI.ParticleRate.Always(10);
                    emitter.Generator = UI.ParticleGenerator.Same(info, UI.ParticlePoints.EngulfFromSide(region.Rect, 2, enemy.IsEnemy ? 2 : 1));
                    Application.CurrentScene.AddEmitter(emitter.Create());
                    Game.Current.Log.Add(Pay.Phrase, attacker.Owner, HowlingSpiritCost, attacker.Card.Element);
                    attacker.Owner.DecrementMana(attacker.Card.Element, HowlingSpiritCost);
                } else if (attacker.HasFlag<Affects.Unbind>()) {
                    mana = true;
                } else {
                    UI.ParticleInfo info = new UI.ParticleInfo(Data.Particles.Shine01);
                    info.AddPhase(20, 20, attacker.Card.Element.ElementColor, 0.2, 0.1, 0, UI.ParticleTrajectory.Line(enemy.IsEnemy ? 90 : -90, 3));
                    info.AddPhase(20, 40, attacker.Card.Element.ElementColor, 0.7, 0.4, 0, UI.ParticleTrajectory.Line(enemy.IsEnemy ? -90 : 90, 0.5));
                    info.AddPhase(12, 0, attacker.Card.Element.ElementColor, 0.1, 0.2);
                    UI.ParticleEmitterInfo emitter = new UI.ParticleEmitterInfo();
                    emitter.Duration = 70;
                    emitter.Rate = UI.ParticleRate.Always(10);
                    emitter.Generator = UI.ParticleGenerator.Same(info, UI.ParticlePoints.EngulfFromSide(region.Rect, 2, enemy.IsEnemy ? 2 : 1));
                    Application.CurrentScene.AddEmitter(emitter.Create());
                    Game.Current.Log.Add(NotPay.Phrase, attacker.Owner);
                }
                canAttack = canAttack && mana;
            }
        }
    }
}
